<!-- 修改密码 -->
<template>
  <div>
    <div>
      <van-cell-group>
        <van-field v-model="data.password" required ref="password" label="原始密码" type="password" placeholder="请输入原始密码" />
      </van-cell-group>
      <van-cell-group>
        <van-field v-model="data.newPassword" required ref="newPassword" label="新密码" type="password" placeholder="请输入新密码" />
      </van-cell-group>
      <van-cell-group>
        <van-field v-model="confirmPsw" required ref="confirmPsw" label="确认密码" type="password" placeholder="请确认密码" />
      </van-cell-group>

      <van-button :loading="loading" type="info" class="confirm" @click="save" loading-text="加载中...">立即修改</van-button>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      isDisabled: false,
      loading: false,
      data: {
        newPassword: '',//新密码
        password: '',//原密码
      },
      confirmPsw: '',
    }
  },
  methods: {
    //立即注册
    async save () {
      if (this.data.password == '') {
        this.$notify({ type: 'warning', message: '原始密码不能为空' });
        this.$refs.password.focus();
        return;
      }
      if (!this.data.newPassword) {
        this.$notify({ type: 'warning', message: '新密码不能为空' });
        this.$refs.newPassword.focus();
        return;
      }
      let regu = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/
      let re = new RegExp(regu)
      if (!re.test(this.data.newPassword)) {
        this.$notify({ type: 'warning', message: '新密码必须由6-16位数字和字母组成' });
        this.$refs.newPassword.focus();
        return;
      }
      if (this.confirmPsw == '') {
        this.$notify({ type: 'warning', message: '确认密码不能为空' });
        this.$refs.confirmPsw.focus();
        return;
      }
      if (this.data.newPassword != this.confirmPsw) {
        this.$notify({ type: 'warning', message: '两次密码不一致' });
        return;
      }
      this.loading = true
      const url = '/user/modifyPwd'
      let result = await this.request.mobileRequest('PUT', url, this.data)
      if (result.code == 200) {
        this.$notify({ type: 'success', message: result.msg });
        localStorage.clear()
        this.$router.push('/')
        this.loading = false
      } else {
        this.loading = false
        this.$notify({ type: 'warning', message: result.msg });
      }
    }
  },

}
</script>

<style scoped>
.confirm {
  margin-top: 0.5rem;
  width: 90%;
  margin-left: 5%;
  background-color: #ff5252;
  border: none;
}
.van-cell {
  margin-bottom: 0.25rem !important;
}
</style>
